package com.ht.medicaltreatment

import com.blankj.utilcode.util.UiMessageUtils
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.serial.SerialPortUtils
import com.ht.medicaltreatment.utils.CRCUtils
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.test_layout.close
import kotlinx.android.synthetic.main.test_layout.open
import kotlinx.android.synthetic.main.test_layout.tvContent
import kotlinx.android.synthetic.main.test_layout.tvSend
import kotlinx.android.synthetic.main.test_layout.uhfRead
import kotlinx.android.synthetic.main.test_layout.uhfSend
import kotlinx.android.synthetic.main.test_layout.uhfStop

class SerialProtTestActivity:BaseActivity() {
    override fun getLayoutId() = R.layout.test_layout

    override fun init() {

        UiMessageUtils.getInstance().addListener {
            tvContent.text = it.`object`.toString()
        }

        open.setOnClickListener {
            showToast("开启")
            SerialPortUtils.initDevice()
        }
        close.setOnClickListener {
            showToast("关闭")
            SerialPortUtils.close()
        }

        val cmds = byteArrayOf(0x3A.toByte(), 0x03.toByte(), 0x00.toByte(), 0x07.toByte())
        val send = CRCUtils.byteAddToByte(cmds,CRCUtils.getCrcData(cmds))

        val UHFSave = byteArrayOf(0x3A.toByte(), 0x04.toByte(), 0x00.toByte(), 0x30.toByte(), 0x03.toByte())
        val sendUHF = CRCUtils.byteAddToByte(UHFSave,CRCUtils.getCrcData(UHFSave))

        val UHFStop = byteArrayOf(0x3A.toByte(), 0x03.toByte(), 0x00.toByte(), 0x31.toByte())
        val stopUHF = CRCUtils.byteAddToByte(UHFStop,CRCUtils.getCrcData(UHFStop))

        val UHFRead = byteArrayOf(0x3A.toByte(), 0x03.toByte(), 0x00.toByte(), 0x32.toByte())
        val readUHF = CRCUtils.byteAddToByte(UHFRead,CRCUtils.getCrcData(UHFRead))
        //val data = CRCUtils.bytes2HexString(send)

        //LogUtils.i(data)
        //3a 09 00 07   [01 00 00 00 00 00] ca

        //读取UHF指令 3A04003003F2
        //停止UHF指令 3A030031F7
        //获取UHF指令 3A030032F4

        //运行状态
        tvSend.setOnClickListener {
            showToast("获取运行状态")
            SerialPortUtils.mSerialPortManager.sendBytes(send)
        }

        uhfSend.setOnClickListener {
            SerialPortUtils.mSerialPortManager.sendBytes(sendUHF)
        }

        uhfStop.setOnClickListener {
            SerialPortUtils.mSerialPortManager.sendBytes(stopUHF)
        }

        uhfRead.setOnClickListener {
            SerialPortUtils.mSerialPortManager.sendBytes(readUHF)
        }
    }
}